package com.zmn.oms.business.impl.log;

import com.zmn.common.utils.number.NumberUtil;
import com.zmn.oms.business.interfaces.log.OrderLogMessageBService;
import com.zmn.oms.model.entity.log.OrderMessageLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类描述：
 *
 * @author yule
 * @date 2020/10/30 19:28
 */
@Service("orderLogMessageBService")
public class OrderMessageLogBServiceImpl implements OrderLogMessageBService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public void save(OrderMessageLog orderMessageLog) {
        mongoTemplate.save(orderMessageLog);
    }

    @Override
    public List<OrderMessageLog> listByOrderIdAndGroupId(Long orderId, Integer groupId) {
        Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.and("orderId").is(orderId);
        if (NumberUtil.isNotNullOrZero(groupId)) {
            criteria.and("groupId").is(groupId);
        }
        query.addCriteria(criteria);
        query.with(Sort.by(Sort.Order.desc("createTime")));
        return mongoTemplate.find(query, OrderMessageLog.class);
    }
}
